package com.zwy.ubts.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yg.yws.annotation.YWebPath;
import com.yg.yws.annotation.YWebServlet;
import com.yg.yws.beans.RetType;
import com.zwy.ubts.bean.Address;
import com.zwy.ubts.bean.Order;
import com.zwy.ubts.bean.User;
import com.zwy.ubts.service.AddressService;
import com.zwy.ubts.service.OrderService;
import com.zwy.ubts.service.UserService;
import com.zwy.ubts.service.impl.AddressServiceImpl;
import com.zwy.ubts.service.impl.OrderServiceImpl;
import com.zwy.ubts.service.impl.UserServiceImpl;
import com.zwy.ubts.utils.DataInfo;
import com.zwy.ubts.utils.RetJsonUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Zhou Wenying
 * @Description:
 * @Date Created in 14:59 2023/3/31
 */
@YWebServlet
public class OrderController {
    private OrderService orderService = new OrderServiceImpl();

    @YWebPath("/insertOrder")
    public RetType insertOrder(String json, HttpSession session){
        User user = (User) session.getAttribute("user");
        System.out.println(json);
        Gson gson = new Gson();
        Type type = new TypeToken<Map<String, String>>() {}.getType();
        Map<String, String> reqMap = gson.fromJson(json, type);

        String addrid = reqMap.get("addrid");
        String bookid = reqMap.get("bookid");
        String[] arrayBook = bookid.split("&");
        System.out.println(Arrays.toString(arrayBook));

        Map map = new HashMap();
        int ret = orderService.insertOrder(user.getUserId(), addrid, arrayBook);
        UserService userService = new UserServiceImpl();
        session.setAttribute("user",userService.selectUserById(user.getUserId()));

        return RetType.retJson(RetJsonUtils.toJson(ret));
    }

    @YWebPath("/myOrder")
    public RetType myOrder(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Order> buyOrders = orderService.selectOrderByOwnerUserId(user.getUserId());
        List<Order> sellOrders = orderService.selectOrderByOtherUserId(user.getUserId());
        Map map = new HashMap();
        map.put("buyOrders",buyOrders);
        map.put("sellOrders",sellOrders);
        return RetType.retJson(RetJsonUtils.toJson(map));
    }

    @YWebPath("/updateSellInfo")
    public RetType updateSellInfo(Integer orderId,String kdId,Integer bookId,HttpSession session){
        User user = (User) session.getAttribute("user");
        orderService.updateBookIdById(user.getUserId(),orderId,bookId,kdId);
        Map map = new HashMap();
        return RetType.retJson(RetJsonUtils.toJson(map));
    }

    //我的售卖
    @YWebPath("/myOrderSell")
    public RetType myOrderSell(HttpSession session){
        User user = (User) session.getAttribute("user");

        List<Order> sellOrders = orderService.selectOrderByOtherUserId(user.getUserId());

        Map map = new HashMap();
        map.put("sellOrders",sellOrders);

        return RetType.retForward(DataInfo.PAGE_ROOT+"myOrderSell.jsp",map);
    }

    @YWebPath("/doMyOrderSell")
    public RetType doMyOrderSell(HttpSession session,Integer orderId,Integer bookId,String ydh){
        User user = (User) session.getAttribute("user");
        Integer integer = orderService.updateBookIdById(user.getUserId(), orderId, bookId, ydh);
        return RetType.retJson(RetJsonUtils.toJson("订单号添加成功！"));
    }


    //我的购买
    @YWebPath("/myOrderBuy")
    public RetType myOrderBuy(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Order> buyOrders = orderService.selectOrderByOwnerUserId(user.getUserId());
        Map map = new HashMap();
        map.put("buyOrders",buyOrders);
        return RetType.retForward(DataInfo.PAGE_ROOT+"myOrderBuy.jsp",map);
    }

    @YWebPath("/doMyOrderBuy")
    public RetType doMyOrderBuy(HttpSession session,Integer orderId,Integer bookId){
        User user = (User) session.getAttribute("user");
        Integer integer = orderService.updateBookById(user.getUserId(),orderId,bookId);

        List<Order> buyOrders = orderService.selectOrderByOwnerUserId(user.getUserId());
        Map map = new HashMap();
        map.put("buyOrders",buyOrders);
        return RetType.retForward(DataInfo.PAGE_ROOT+"myOrderBuy.jsp",map);
    }

}

